import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_main/se_video/record/se_video_record_page.dart';
import 'package:squat_easy/se_route.dart';
import 'package:squat_easy/se_ui/se_dialogs.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import 'se_video_logic.dart';

class SeVideoPage extends StatefulWidget {
  const SeVideoPage(this.logic, {Key? key}) : super(key: key);

  final SeVideoLogic logic;

  @override
  State<SeVideoPage> createState() => _SeVideoPageState();
}

class _SeVideoPageState extends State<SeVideoPage> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _SeVideoPage(widget.logic);
  }

  @override
  bool get wantKeepAlive => true;
}

class _SeVideoPage extends StatelessWidget {
  const _SeVideoPage(this.logic);

  final SeVideoLogic logic;

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      hideBackBtn: true,
      child: Stack(
        fit: StackFit.expand,
        children: [
          _buildBg().positioned(top: 0, left: 0, right: 0, bottom: 0),
          _buildRecordBtn().paddingBottom(50.w).alignBottom(),
        ],
      ),
    );
  }

  Widget _buildBg() => const IconWidget.image(Assets.pngTabVideoBg, fit: BoxFit.fill);

  Widget _buildRecordBtn() {
    return ButtonWidget.icon(
      ImageWidget.asset(Assets.pngVideoStartRecord, width: 256.w, height: 112.w),
      onTap: () async {
        CommonPrivilegeStatus status = await CommonPrivilege.camera();
        if (!status.result) {
          SeDialogs.twoButton(
            title: status.title,
            description: status.message,
            cancelTitle: YlmGlobalConfig().actionSheetConfig.cancelTitle,
            confirmTitle: YlmGlobalConfig().actionSheetConfig.confirmTitle,
            onConfirm: () => CommonPrivilege.openSettings(),
          );
          return;
        }

        CommonPrivilegeStatus microphoneStatus = await CommonPrivilege.microphone();
        if (!microphoneStatus.result) {
          SeDialogs.twoButton(
            title: status.title,
            description: status.message,
            cancelTitle: YlmGlobalConfig().actionSheetConfig.cancelTitle,
            confirmTitle: YlmGlobalConfig().actionSheetConfig.confirmTitle,
            onConfirm: () => CommonPrivilege.openSettings(),
          );
          return;
        }

        SeRoute.open(const SeVideoRecordPage());
      },
    );
  }
}
